package ua.pp.shell.wka;

import java.util.ArrayList;
import java.util.List;
import ua.pp.shell.wka.DBAdapter.Types.Word;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class WordListAdapter extends ArrayAdapter<Word>{
	private Context context;
	private DBAdapter dbAdapter = null;
	private Activity activity;
    private LayoutInflater inflater = null;
    private ArrayList<Word> selectedItems = new ArrayList<Word>();
    public boolean reversed = false;
    
	public WordListAdapter(Activity activity, int textViewResourceId,
			List<Word> objects) {
		super(activity, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.context = (Context)activity;
		dbAdapter = new DBAdapter(context);
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	    
	public void DeleteSelected() {
		for(int i = 0; i < this.selectedItems.size(); i++ ) {
			dbAdapter.DeleteWord(this.selectedItems.get(i).id);
			this.remove(this.selectedItems.get(i));
		}
		this.selectedItems.clear();
		this.notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Word word = getItem(position);
		String str_to_show = "";
		if (reversed) {
			str_to_show = word.translation;
		} else {
			str_to_show = word.word;
		}
	    if(convertView == null) {
	    	convertView = inflater.inflate(R.layout.checkbox_lvitem, null);
	    	CheckBox checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
	        ImageView edit = (ImageView)convertView.findViewById(R.id.edit);
	        TextView word_text = (TextView)convertView.findViewById(R.id.title);
	        word_text.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					Word word_tag = (Word)view.getTag();
					String str_to_show = "";
					if (reversed) {
						str_to_show = word_tag.word;
					} else {
						str_to_show = word_tag.translation;
					}
					Toast.makeText(getContext(), str_to_show, 2000).show();
				}
			});
	        
	        edit.setOnClickListener(new OnClickListener() {
	        	@Override
	        	public void onClick(View v) {
	        		Intent intent = new Intent(context, AEWordActivity.class);
					Word word = (Word)v.getTag();
		        	intent.putExtra("id", word.id);
		        	activity.startActivityForResult(intent, App.activities.AE_WORD.ordinal());
				}
			});
	        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
	        	@Override
				public void onCheckedChanged(CompoundButton view, boolean isChecked) {
					CheckBox cBox = (CheckBox) view;
					Word tag = (Word) cBox.getTag();
					if (cBox.isChecked()) {
						if (!selectedItems.contains(tag))
							selectedItems.add(tag);
					} else {
					if (selectedItems.contains(tag))
						selectedItems.remove(tag);
					}
				}
	        });
	    }	
	    TextView et = ((TextView)convertView.findViewById(R.id.title));
        et.setText(str_to_show);
        CheckBox checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
        checkbox.setTag(word);
        checkbox.setChecked(false);
        ImageView edit = (ImageView)convertView.findViewById(R.id.edit);
        TextView word_text = (TextView)convertView.findViewById(R.id.title);
        word_text.setTag(word);
        edit.setTag(word);
	    return convertView;
	}
	
	public void AddWord(Word word) {
		if ( word.id < 0 ) return;
		this.add(word);
		this.notifyDataSetChanged();
	}
	
	public void UpdateWord(Word word) {
		for( int i=0; i < this.getCount(); i++ ) {
			Word current_word = (Word)this.getItem(i);
			if ( current_word.id == word.id ) {
				this.remove(current_word);
				this.insert(word, i);
				this.notifyDataSetChanged();
				return;
			}
			continue;
		}
	}
}

